package me.youline.dataServer.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import me.youline.dataServer.dao.OfficialUserRecordDAO;
import me.youline.dataServer.entity.OfficialUserRecord;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 官方号用户业务类
 * @author linhan
 *
 */
@Component
public class OfficialUserService {

	@Autowired
	private OfficialUserRecordDAO recordDAO;
	
	private Set<Long> userIds = new HashSet<Long>(30);
	
	private Set<Long> followedUserId = new HashSet<Long>(30);
	
	/**
	 * 初始化所有官方号列表，及默认关注官方号
	 */
	@PostConstruct
	protected void initialize(){
		List<OfficialUserRecord> list = recordDAO.loadOfficalUserIds();
		for(OfficialUserRecord r : list){
			doFollowedUserProcess(r);
			userIds.add(r.getUserId());
		}
	}
	
	
	/**
	 * 将需要被用户默认关注的官方号记录号抽取出来并单独存储
	 * @param r
	 */
	private void doFollowedUserProcess(OfficialUserRecord r){
		if(r.isFollowed()){
			followedUserId.add(r.getUserId());
		}
	}
	
	/**
	 * 判断用户是否为官方号
	 * @param userId
	 * @return
	 */
	public boolean isOfficalUser(Long userId){
		return userIds.contains(userId);
	}
	
	
	/**
	 * 获取所有官方号id
	 * @return
	 */
	public Collection<Long> getOfficialUsers(){
		return new ArrayList<Long>(userIds);
	}
	
	/**
	 * 获取用户默认关注的官方号列表
	 * @return
	 */
	public Collection<Long> getFollowedOfficialUsers(){
		return this.followedUserId;
	}
}
